# -*- coding: utf-8 -*-
'''
Created on Jan 4, 2013

@author: LONG HOANG GIANG
'''

from CrawlerLib2 import commonlib
import os, datetime
import json


class SohaBooksApi():
    
    apiUrl = 'http://vcmtools.8x01.vn/sohabook/apigetType.php'
    userAgent = 'android-async-http/1.4.1 (http://loopj.com/android-async-http)'
    
    def __init__(self):
        pass
    
    
    def getListCat(self):
        
        data = {'action': 'getcatlist'}
        data = commonlib.loadweb(self.apiUrl, data=data, useragent=self.userAgent).gethtml()
        a = json.loads(data)
        categories = a['ListCat']
        for cat in categories:
            print cat['id'], cat['name']
            
    def getHot(self):
        
        data = {'action': 'booktype', 'type': 'hot'}
        data = commonlib.loadweb(self.apiUrl, data=data, useragent=self.userAgent).gethtml()
        a = json.loads(data)
        books = a['BookType']
        for book in books:
            print book['id'], book['name'], book['fileepub']
        
    def search(self, kw):
        
        data = {'action': 'search', 'type': '0', 'keyword': kw}
        data = commonlib.loadweb(self.apiUrl, data=data, useragent=self.userAgent).gethtml()
        a = json.loads(data)
        print a
        
if __name__ == '__main__':
    
    soha = SohaBooksApi()
#    soha.getListCat()
    
    soha.getHot()
    soha.search('xin loi em chi la')
    
    
    
    